package com.enzo.prms.common.interceptor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.enzo.prms.common.constant.Constant;

public class MyInterceptor extends AbstractInterceptor {
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation invocation) {
		String result = null;
		invocation.addPreResultListener(new PreResultHandle());
		try {
			System.out
					.println("<<<<<<<<<<<"
							+ invocation.getAction().getClass().getName()
							+ ">>>>>>>>>");
			result = invocation.invoke();
			System.out
					.println("<<<<<<<<<<<"
							+ invocation.getAction().getClass().getName()
							+ ">>>>>>>>>");
		} catch (Exception e) {
			System.out.println("Service Exception.....");
			try {
				Object ac = invocation.getAction();
				Class<? extends Object> cl = ac.getClass();
				Method method = cl.getMethod("setMessage", String.class);
				// getMethod("setMessage");
				method.invoke(ac, Constant.MSG_SERVICEEXCEPTION_DUPLICATE);
			} catch (SecurityException e1) {
				e1.printStackTrace();
			} catch (NoSuchMethodException e1) {
				e1.printStackTrace();
			} catch (IllegalArgumentException e2) {
				e2.printStackTrace();
			} catch (IllegalAccessException e3) {
				e3.printStackTrace();
			} catch (InvocationTargetException e4) {
				e4.printStackTrace();
			}

			result = Action.ERROR;

			e.printStackTrace();
		}
		return result;
	}

}
